$(function(){
	if($('.news_block .item').size() <=3){
		$('.scroller').hide();
		
	}else{
	//scrollpane parts
	var scrollPane = $(".scroll-pane");
	var	scrollContent = $(".scroll-content");
	$('.news_block .item:last').addClass('last');
	var marg=parseInt($('.news_block .item').css('margin-right'));
	var itemWidth=$('.news_block .item').width()+marg;
	scrollContentWidth=parseInt(itemWidth*$('.news_block .item').size());
	scrollContent.width(scrollContentWidth-marg);
	
	//build slider
	var scrollbar = $( ".scroll-bar" ).slider({
		slide: function( event, ui ) {
			if ( scrollContent.width() > scrollPane.width() ) {
				scrollContent.css( "margin-left", Math.round(
					ui.value / 100 * ( scrollPane.width() - scrollContent.width() )
				) + "px" );
			} else {
				scrollContent.css( "margin-left", 0 );
			}
		}
	});
	
	
	//append icon to handle
	var handleHelper = scrollbar.find( ".ui-slider-handle" );
	handleHelper.append('<table><tr><td class="left">&nbsp;</td><td>&nbsp;</td><td class="right">&nbsp;</td></tr></table>');
	//change overflow to hidden now that slider handles the scrolling
	scrollPane.css( "overflow", "hidden" );
	
	//size scrollbar and handle proportionally to scroll distance
	function sizeScrollbar() {
		
		var remainder = scrollContent.width() - scrollPane.width();
		var proportion = remainder / scrollContent.width();
		var handleSize = scrollPane.width() - ( proportion * scrollPane.width())-34;
		var wid=scrollbar.width() - handleSize;
		scrollbar.find( ".ui-slider-handle").css({
			width: Math.round(handleSize),
			"margin-left": -Math.round(handleSize/2)
		});
		
		$('.scroll-bar-wrap').css('padding-left',Math.round(handleSize/2));
		$('.scroll-bar-wrap').css('padding-right',Math.round(handleSize/2));
	}
	
	//reset slider value based on scroll content position
	function resetValue() {
		var remainder = scrollPane.width() - scrollContent.width();
		var leftVal = scrollContent.css( "margin-left" ) === "auto" ? 0 :
			parseInt( scrollContent.css( "margin-left" ) );
		var percentage = Math.round( leftVal / remainder * 100 );
		scrollbar.slider( "value", percentage );
	}
	
	//if the slider is 100% and window gets larger, reveal content
	function reflowContent() {
			var showing = scrollContent.width() + parseInt( scrollContent.css( "margin-left" ), 10 );
			var gap = scrollPane.width() - showing;
			if ( gap > 0 ) {
				scrollContent.css( "margin-left", parseInt( scrollContent.css( "margin-left" ), 10 ) + gap );
			}
	}
	
	$('.button-left').click(function(){
		val=parseInt(scrollbar.slider( "value"));
		if((val-20)>=0){
			scrollbar.slider( "value",val-20);
			if ( scrollContent.width() > scrollPane.width() ) {
				scrollContent.css( "margin-left", Math.round(
				(val-20) / 100 * ( scrollPane.width() - scrollContent.width() )
				) + "px" );
			} else {
				scrollContent.css( "margin-left", 0 );
			}
			
		}else{
			scrollbar.slider( "value",0);
			scrollContent.css( "margin-left", 0 );
		}
		return false;
	});
	
	$('.button-right').click(function(){
		val=parseInt(scrollbar.slider( "value"));
		if((val+20)<=100){
			scrollbar.slider( "value",val+20);
			if ( scrollContent.width() > scrollPane.width() ) {
				scrollContent.css( "margin-left", Math.round(
				(val+20) / 100 * ( scrollPane.width() - scrollContent.width() )
				) + "px" );
			} else {
				scrollContent.css( "margin-left", 0 );
			}
			
		}else{
			scrollbar.slider( "value",100);
			if ( scrollContent.width() > scrollPane.width() ) {
				scrollContent.css( "margin-left", Math.round(
				(100) / 100 * ( scrollPane.width() - scrollContent.width() )
				) + "px" );
			} else {
				scrollContent.css( "margin-left", 0 );
			}
			
		}
		return false;
	});
	
	
	//change handle position on window resize
	$( window ).resize(function() {
		resetValue();
		sizeScrollbar();
		reflowContent();
	});
	//init scrollbar size
	setTimeout( sizeScrollbar, 10 );//safari wants a timeout

	}
	
	$(".news-gallery-wrapper .jcarousel").jCarouselLite({
	    btnNext: ".news-gallery-wrapper .next",
	    btnPrev: ".news-gallery-wrapper .prev",
	    visible: 7
	});
	
});